<?php

namespace App\Http\Controllers\Admin\Customer;

use App\Http\Controllers\Controller;
use App\Http\Requests\customer\NodeRequest;
use App\Models\customer\ApiModel;
use App\Models\customer\NodeModel;
use Illuminate\Http\Request;

class Node extends Controller
{
    use NodeRequest;

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $name = $request->input('name', '');
        $type = $request->input('type', 0);
        $customer_id = $request->input('customer_id', '');
        $create_start = $request->input('create_start', '');
        $create_end = $request->input('create_end', '');

        $query = NodeModel::query()->with(['create_admin', 'customer']);

        if($name){
            $query = $query->where('name_cn|name_en', 'like', '%'.$name.'%');
        }

        if($customer_id){
            $query = $query->where('customer_id', $customer_id);
        }

        if($type){
            $query = $query->where('type', $type);
        }

        if($create_start && $create_end){
            $query = $query->whereBetween('created_at', [$create_start, $create_end]);
        }

        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->get();

        $list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'customer_id' => $item->customer_id,
                'customer_name' => $item->customer->name ?? '',
                'name_cn' => $item->name_cn,
                'name_en' => $item->name_en,
                'type' => $item->type,
                'api_id' => $item->api_id,
                'api' => $item->api,
                'status' => $item->status,
                'created_at' => $item->created_at->toDateTimeString()
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }

    /**
     * Notes:添加
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request){
        $auth = $request->get('auth');
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $api = ApiModel::query()->where('id', $params['api_id'])->first();

        $model = new NodeModel();
        $model->customer_id = $params['customer_id'];
        $model->name_cn = $params['name_cn'];
        $model->name_en = $params['name_en'];
        $model->type = $api->type;
        $model->api_id = $params['api_id'];
        $model->api = $api->api;
        $model->status = $params['status'] ?? '';
        $model->created_admin_id = $auth->id;
        $model->save();

        return $this->success();
    }

    public function update(Request $request, $id){
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $model = NodeModel::find($id);
        if (!$model) {
            return $this->error('数据不存在');
        }

        $api = ApiModel::query()->where('id', $params['api_id'])->first();

        $model->customer_id = $params['customer_id'];
        $model->name_cn = $params['name_cn'];
        $model->name_en = $params['name_en'];
        $model->type = $api->type;
        $model->api_id = $params['api_id'];
        $model->api = $api->api;
        $model->status = $params['status'] ?? '';
        $model->save();

        return $this->success();
    }


    public function delete(int $id){
        $model = NodeModel::find($id);
        if(!$model) {
            return $this->error('记录不存在');
        }
        $model->delete();
        return $this->success();
    }

}
